import type { BaseResponse } from "@/api/response";
import { http } from "@/utils/http";

export class RelayServer {
  customResourceDefinitionUrl = cluster_name => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/apiextensions.k8s.io/v1/customresourcedefinitions`;
  };

  listCustomResourceDefinitions(
    cluster_name?: string,
    search?: any,
    keywords?: any,
    pageNum?: any,
    pageSize?: any
  ): BaseResponse<any> {
    let url = this.customResourceDefinitionUrl(cluster_name);
    const params = {};
    if (search) {
      params["search"] = true;
      if (keywords) {
        params["keywords"] = keywords;
      }
      if (pageNum && pageSize) {
        params["pageNum"] = pageNum;
        params["pageSize"] = pageSize;
      }
    }
    return http.get(url, params);
  }

  deleteCustomResourceDefinition(cluster_name, name) {
    return http.delete(
      `${this.customResourceDefinitionUrl(cluster_name)}/${name}`
    );
  }

  getCustomResourceDefinition(cluster_name, name) {
    return http.get(
      `${this.customResourceDefinitionUrl(cluster_name)}/${name}`
    );
  }

  createCustomResourceDefinition(cluster_name, data) {
    return http.post(`${this.customResourceDefinitionUrl(cluster_name)}`, data);
  }

  updateCustomResourceDefinitione(cluster_name, name, data) {
    return http.put(
      `${this.customResourceDefinitionUrl(cluster_name)}/${name}`,
      data
    );
  }

  resourceUrl = (cluster_name, version, group, resource_name) => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/${group}/${version}/${resource_name}`;
  };
  resourceNSUrl = (cluster_name, version, group, resource_name, namespace) => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/${group}/${version}/namespaces/${namespace}/${resource_name}`;
  };

  listResourceByGroup(
    cluster_name,
    version,
    group,
    resource_name,
    search,
    keywords,
    pageNum,
    pageSize
  ) {
    let url = this.resourceUrl(cluster_name, version, group, resource_name);
    const params = {};
    if (search) {
      params["search"] = true;
      if (keywords) {
        params["keywords"] = keywords;
      }
      if (pageNum && pageSize) {
        params["pageNum"] = pageNum;
        params["pageSize"] = pageSize;
      }
    }
    return http.get(url, params);
  }

  deleteResource(cluster_name, version, group, names, namespace, name) {
    let url = this.resourceUrl(cluster_name, version, group, names);
    if (namespace && namespace !== "") {
      url = this.resourceNSUrl(cluster_name, version, group, names, namespace);
    }
    return http.delete(`${url}/${name}`);
  }

  getResource(cluster_name, version, group, names, namespace, name) {
    let url = this.resourceUrl(cluster_name, version, group, names);
    if (namespace && namespace !== "") {
      url = this.resourceNSUrl(cluster_name, version, group, names, namespace);
    }
    return http.get(`${url}/${name}`);
  }

  updateResource(cluster_name, version, group, names, namespace, name, data) {
    let url = this.resourceUrl(cluster_name, version, group, names);
    if (namespace && namespace !== "") {
      url = this.resourceNSUrl(cluster_name, version, group, names, namespace);
    }
    return http.put(`${url}/${name}`, data);
  }
}
